import java.util.LinkedList;
import java.util.Queue;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: Administrator
 * \* Date: 2022/2/3
 * \* Time: 14:26
 * \* To change this template use File | Settings | File Templates.
 * \* Description:
 * \
 */

class BTNode{
    public char val;
    public BTNode left;//左孩子的引用
    public BTNode right;//右孩子的引用
    public BTNode (char val){
        this.val=val;
    }
}
public class BinaryTree {
    public BTNode root;//二叉树的根节点

    public BTNode createTree(){
        BTNode A = new BTNode('A');
        BTNode B = new BTNode('B');
        BTNode C = new BTNode('C');
        BTNode D = new BTNode('D');
        BTNode E = new BTNode('E');
        BTNode F = new BTNode('F');
        BTNode G = new BTNode('G');
        BTNode H = new BTNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;
    }

    void preOrder(BTNode root){
        if(root == null){
            return;
        }
        System.out.print(root.val+" ");
        preOrder(root.left);
        preOrder(root.right);
    }
//中序遍历
    void inOrder(BTNode root){
        if(root == null){
            return;
        }
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }

//后序遍历
    void postOrder(BTNode root){
        if(root == null){
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val+" ");
    }

    // 获取树中节点的个数
    int count = 0;
    int size1(BTNode root){
        if(root == null){
            return 0;
        }
        count++;
        size1(root.left);
        size1(root.right);
        return count;
    }

    // 求整棵树root的节点个数
    int size(BTNode root){
        if(root == null){
            return 0;
        }
        return size(root.left) + size(root.right) + 1;
    }


    // 获取叶子节点的个数
    int getLeafNodeCount(BTNode root){
        if(root == null){
            return 0;
        }
        if(root.left == null && root.right == null){
            return 1;
        }else{
            return getLeafNodeCount(root.left) + getLeafNodeCount(root.right);
        }
    }
    /**
     * 获取第K层节点的个数
     * 子问题思路：
     *
     */
    int getKLevelNodeCount(BTNode root,int k) {
        if(root == null || k <= 0) {
            return 0;
        }
        if(k == 1) {
            return 1;
        }
        return getKLevelNodeCount(root.left,k-1) + getKLevelNodeCount(root.right,k-1);
    }

    /**
     * 获取二叉树的高度
     * 时间复杂度：O(n)
     * 空间复杂度：O()
     */
    int getHeight(BTNode root) {
        if(root == null) return 0;
        int leftHeight = getHeight(root.left);
        int rightHeight = getHeight(root.right);
        return leftHeight > rightHeight ? leftHeight+1 : rightHeight+1;
    }

    int getHeight2(BTNode root) {
        if(root == null) return 0;
        return getHeight2(root.left) > getHeight2(root.right) ?
                getHeight2(root.left)+1 : getHeight2(root.right)+1;
    }

    /**
     * 检测值为value的元素是否存在
     */
    BTNode find(BTNode root, char val) {
        if(root == null) return null;
        if(root.val == val) return root;

        BTNode ret = find(root.left,val);
        if(ret != null) {
            return ret;
        }
        ret = find(root.right,val);
        if(ret != null) {
            return ret;
        }
        return null;
    }

    /**
     * 是不是完全二叉树
     * @param root
     * @return
     */
    boolean isCompleteTree(BTNode root) {
        if(root == null) return true;
        Queue<BTNode> queue = new LinkedList<>( );
        queue.offer(root);
        while (!queue.isEmpty()) {
            BTNode cur = queue.poll();
            if(cur != null) {
                queue.offer(cur.left);
                queue.offer(cur.right);
            }else {
                break;
            }
        }

        while (!queue.isEmpty()) {
            BTNode top = queue.peek();
            if(top != null) {
                return false;
            }
            queue.poll();
        }
        return true;
    }

}
